package cn.zhizihua.service.impl;

import cn.zhizihua.exception.AppException;
import cn.zhizihua.exception.AppExceptionCodeMsg;
import cn.zhizihua.mapper.BaseMapper;
import cn.zhizihua.query.BaseQuery;
import cn.zhizihua.service.IBaseService;
import cn.zhizihua.vo.PageBean;
import cn.zhizihua.vo.ResultBean;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.List;

/**
 * 通用service实现类
 *
 * BaseService中可以做基础实现
 * 但是模型依然无法确定
 * @param <T>
 */
@Service
public class BaseService<T> implements IBaseService<T> {
    @Autowired
    BaseMapper<T> baseMapper;

    @Override
    public ResultBean save(T t) {
        /*t 对象 eg：Business
        t的字节码文件类*/
        int i = 0;

        // 1.获取T的字节码文件类
        Class<?> aClass = t.getClass();
        // 2.获取类名  eg：Business
        String className = aClass.getSimpleName();
        // 3.拼接get方法的名字， get+Business+Id
        String getMethodName = "get"+className+"Id";

        try {
            // 4.通过字节码文件类获取到get方法的对象
            Method method = aClass.getMethod(getMethodName);
            // 5.通过反射调用get方法 获取返回值 id值
            Object id = method.invoke(t);

            if (id == null){
                i = baseMapper.add(t);
            }else {
                i = baseMapper.update(t);
            }
        } catch (Exception e) {
            e.printStackTrace();
            throw new AppException();  //抛出自定义异常
        }

        //判断是否保存成功
        if (i == 0){
            throw new AppException(AppExceptionCodeMsg.SAVE_FAIL);
        }
        return ResultBean.SUCCESS();
    }

    @Override
    public ResultBean add(T t) {
        //判断参数是否为空
        if (t == null){
            throw new AppException(AppExceptionCodeMsg.PARAMS_EMPTY);
        }

        int i = baseMapper.add(t); //添加

        //判断添加是否成功
        if (i == 0){
            throw new AppException(AppExceptionCodeMsg.INSERT_FAIL); //添加失败 抛出自定义异常
        }
        return ResultBean.SUCCESS();
    }

    @Override
    public ResultBean update(T t) {
        //参数校验
        if (t == null){
            throw new AppException(AppExceptionCodeMsg.PARAMS_EMPTY);
        }

        int i = baseMapper.update(t); //更新

        //判断更新是否成功
        if (i == 0){
            throw new AppException(AppExceptionCodeMsg.UPDATE_FAIL);
        }
        return ResultBean.SUCCESS();
    }

    @Override
    public ResultBean delete(Integer id) {
        //参数校验
        if (id == null || id == 0){
            throw new AppException(AppExceptionCodeMsg.PARAMS_EMPTY);
        }

        int i = baseMapper.delete(id); //删除操作

        //判断删除是否成功
        if (i == 0){
            throw new AppException(AppExceptionCodeMsg.DELETE_FAIL);
        }
        return ResultBean.SUCCESS();
    }

    @Override
    public ResultBean getById(Integer id) {
        //参数校验
        if (id == null || id == 0){
            throw new AppException(AppExceptionCodeMsg.PARAMS_EMPTY);
        }

        T t = baseMapper.getById(id); //通过id查询

//        return t==null?ResultBean.ERROR("未找到"):ResultBean.SUCCESS(t);
        if (t == null){
            throw new AppException(AppExceptionCodeMsg.DATA_NOT_FOUND);
        }
        return ResultBean.SUCCESS(t);
    }

    @Override
    public ResultBean getAll(BaseQuery baseQuery) {

        //参数校验
        if (baseQuery == null){
            throw new AppException(AppExceptionCodeMsg.PARAMS_EMPTY);
        }

        //使用分页插件
        Page<T> page = PageHelper.startPage(baseQuery.getPageNow(), baseQuery.getPageCount());
        //查寻全部数据 & 设置分页条件 & baseQuery携带搜索信息
        List<T> list = baseMapper.getAll(baseQuery);

        PageBean<T> pageBean = new PageBean<>();
        pageBean.setTotalRows(page.getTotal());
        pageBean.setData(list);

        return ResultBean.SUCCESS(pageBean);
    }

    @Override
    public ResultBean batchDel(List<Integer> ids) {

        if (ids==null || ids.size()==0){
            throw new AppException(AppExceptionCodeMsg.PARAMS_EMPTY);
        }

        for (Integer id:ids) {
            int delete = baseMapper.delete(id);
            if (delete==0){
                throw new AppException(AppExceptionCodeMsg.DELETE_FAIL);
            }
        }

        return ResultBean.SUCCESS();
    }
}
